 package com.es.util.upload;
 
 import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
 
 @SuppressWarnings("serial")
public class BaoxiaoUpload extends HttpServlet{
	 

	 public void init(ServletConfig servletConfig) throws ServletException {
		    super.init(servletConfig);

		    if(System.getProperty("appDir")==null){
			    String appDir = servletConfig.getServletContext().getRealPath("/");
			    System.getProperties().put("appDir", appDir);
		    }
	 }		    

	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     SmartUpload mySmartUpload = new SmartUpload();
     mySmartUpload.initialize(getServletConfig(), request, response);
   
     String imp_name = "";
     String action_url = "";
     String filenames = "";
     String error="";
     Boolean hasError = false;
     try{
    	 mySmartUpload.upload();
    	 Request req = mySmartUpload.getRequest();
  
    	 String emp_code = req.getParameter("ctm_code1");
    	 String baoxiao_date = req.getParameter("baoxiao_date1");
    	 
    	 for (int i = 0; i < mySmartUpload.getFiles().getCount(); ++i){
    		 File file = mySmartUpload.getFiles().getFile(i);
       		 if (!file.isMissing()){    			
    			 String ext = mySmartUpload.getFiles().getFile(i).getFileExt();
    			 if (file.getSize() > 4194304){
    				 error = "文件大于4M，请处理后再次发送！";
			         hasError = true;
			         break;
    			 }
    			 
    			 //一个月每个ctm仅仅保留一个附件,创建目录，删除已有文件
    			 String path = System.getProperty("appDir") + "upload\\baoxiao\\";
    			 java.io.File newFile = new java.io.File(path);
    			 if(!newFile.exists()){
    				 newFile.mkdirs();
    			 }
    			 String fileName = "";
    			 if(ext.length() > 0)
    				 fileName ="attach_"+ emp_code + "_" + baoxiao_date.substring(0, 7) +"." + ext;
    			 else fileName ="attach_"+ emp_code + "_" + baoxiao_date.substring(0, 7);
    			 newFile = new java.io.File(path + fileName);
    			 if(newFile.exists()){
    				 newFile.delete();
    			 }
    			 file.saveAs(System.getProperty("appDir")+"upload\\baoxiao\\" + fileName, mySmartUpload.SAVE_PHYSICAL);
    			 filenames = fileName;
    		 }
    	 }
     }catch (Exception e) {
    	 e.printStackTrace();
     } 
  
     if(hasError){
    	 request.setAttribute("message", error);
    	 request.setAttribute("hasupload", "false");
    	 RequestDispatcher dis = request.getRequestDispatcher("/jsp/hr/baoxiao/error.jsp");
        // RequestDispatcher dis = request.getRequestDispatcher(imp_name);
         dis.forward(request, response);
         return;
     }
    // this.setAttributes(request);
     request.setAttribute("message", "上传成功！");
     request.setAttribute("fileName", filenames);
     request.setAttribute("hasupload", "true");
     RequestDispatcher dis = request.getRequestDispatcher("/jsp/hr/baoxiao/baoxiaoupload.jsp");
     dis.forward(request, response);
   }
 
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException{
     doGet(request, response);
   }
   

  
 }